// 运行时配置

// 全局初始化数据配置，用于 Layout 用户信息和权限初始化
// 更多信息见文档：https://umijs.org/docs/api/runtime-config#getinitialstate
import logo from '@/assets/logo.png';
import MenuFooterRender from '@/components/MenuFooterRender';
import MenuItem from '@/components/MenuItem';
import StorageManager from '@/utils/StorageManager';
import { parseQuery } from '@/utils/tools';
import { history } from '@umijs/max';
import { Link } from 'umi';
import '../global.less';
export async function getInitialState(): Promise<{ name: string }> {
  return { name: '' };
}
const isByQK = !!(window as any).__POWERED_BY_QIANKUN__;

// 动态计算 siderWidth
const calculateSiderWidth = (): number => {
  const screenWidth = window.innerWidth;
  if (screenWidth > 1680) return 128;
  return 112; // 默认最小值
};
export const layout = () => {
  return {
    logo, // layout 的左上角的 logo
    title: '', // layout 的左上角的 title
    menu: {
      locale: false,
    },
    siderWidth: calculateSiderWidth(), // 动态设置侧边菜单宽度
    disableContentMargin: isByQK,
    pure: isByQK, // 是否删除掉所有的自带界面
    menuFooterRender: () => <MenuFooterRender />,
    menuItemRender: (menuItemProps: any, defaultDom: React.ReactNode) => {
      if (menuItemProps.isUrl || !menuItemProps.path) {
        return defaultDom;
      }

      return (
        <Link to={menuItemProps.path}>
          <MenuItem menuItemProps={menuItemProps} />
        </Link>
      );
    },
    onPageChange: () => {
      const { location } = history;
      // const token = StorageManager.getCookie('codemuse_token');
      const userName = StorageManager.getLocalStorageSync('userName');
      const search = history.location.search as any; // 有code参数，说明是sso登录
      const { code } = parseQuery(search);
      console.log('apptsx=>>>>>', code);
      // 如果没有登录，且不在登录页，重定向到登录页
      if (
        !userName &&
        !(location.pathname || '').includes('/code-muse/login')
      ) {
        // history.push(`/login?code=${code || ''}`);
      }
    },
  };
};
